﻿using Microsoft.TeamFoundation.Framework.Client;
using System.Globalization;
using Microsoft.TeamFoundation.Client;

namespace Outlook.WorItems.Extension.Managers.AlertManager
{
    public class SubscriptionEntry
    {
        // Fields
        private string m_alertName;
        private string m_domain;
        private string m_eventName;
        private IEventService m_eventService;
        private string m_filterExpression;
        private bool m_isSubscribed;
        private string[] m_oldEventNames;
        private string m_projectName;
        private Subscription m_subscription;
        private string m_userId;

        // Methods
        public SubscriptionEntry(string alertName, string eventName, string filterExpression, string projectName, string domain)
            : this(alertName, new string[0], eventName, filterExpression, projectName, domain)
        {
        }

        public SubscriptionEntry(string alertName, string[] oldEventNames, string eventName, string filterExpression, string projectName, string domain)
        {
            m_alertName = alertName;
            m_projectName = projectName;
            m_oldEventNames = oldEventNames;
            m_eventName = eventName;
            m_domain = domain;
            m_filterExpression = filterExpression;
            m_userId = GetUserSid();
            Initialize();
        }

        protected virtual string GetSubscriptionName(string eventName)
        {
            return string.Format(CultureInfo.InvariantCulture, "{0}:{1}", new object[] { m_projectName, eventName });
        }

        private string GetUserSid()
        {
            return TfsTeamProjectCollectionFactory.GetTeamProjectCollection(TfsTeamProjectCollection.GetFullyQualifiedUriForName(m_domain)).AuthorizedIdentity.Descriptor.Identifier;
        }

        private void Initialize()
        {
            var teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(TfsTeamProjectCollection.GetFullyQualifiedUriForName(m_domain));
            this.m_eventService = (IEventService)teamProjectCollection.GetService(typeof(IEventService));
            var eventSubscriptions = m_eventService.GetEventSubscriptions(m_userId, GetSubscriptionName(m_eventName));
            if ((eventSubscriptions.Length == 0) && (m_oldEventNames.Length > 0))
            {
                foreach (string str in m_oldEventNames)
                {
                    eventSubscriptions = m_eventService.GetEventSubscriptions(m_userId, GetSubscriptionName(str));
                    if (eventSubscriptions.Length > 0)
                    {
                        break;
                    }
                }
            }
            if (eventSubscriptions.Length >= 1)
            {
                m_subscription = eventSubscriptions[0];
                m_isSubscribed = true;
            }
            else
            {
                m_isSubscribed = false;
            }
        }

        public void UpdateSubscriptionInBis(bool subscribed, string addresses, DeliveryType deliveryType)
        {
            addresses = addresses.Trim();
            var flag = subscribed && !m_isSubscribed;
            var flag2 = !subscribed && m_isSubscribed;
            if ((subscribed && m_isSubscribed) && (((addresses != m_subscription.DeliveryPreference.Address) || (deliveryType != m_subscription.DeliveryPreference.Type)) || (m_eventName != this.m_subscription.EventType)))
            {
                flag = flag2 = true;
            }
            if (flag)
            {
                var preferences = new DeliveryPreference
                {
                    Address = addresses,
                    Type = deliveryType
                };
                var subscriptionName = GetSubscriptionName(m_eventName);
                m_eventService.SubscribeEvent(m_eventName, m_filterExpression, preferences, subscriptionName);
                m_isSubscribed = true;
            }
            if (!flag2) 
                return;
            m_eventService.UnsubscribeEvent(m_subscription.ID);
            if (!flag)
            {
                m_isSubscribed = false;
            }
        }

        // Properties
        public string Addresses
        {
            get
            {
                return !this.m_isSubscribed ? string.Empty : this.m_subscription.DeliveryPreference.Address;
            }
        }

        public string AlertName
        {
            get
            {
                return m_alertName;
            }
        }

        public DeliveryType DeliveryType
        {
            get
            {
                return !m_isSubscribed ? DeliveryType.EmailHtml : m_subscription.DeliveryPreference.Type;
            }
        }

        public string EventName
        {
            get
            {
                return m_eventName;
            }
        }

        public bool IsSubscribed
        {
            get
            {
                return m_isSubscribed;
            }
        }
    }
}
